package cz.uhk.max.util;

import java.io.Serializable;

public class DataScroller implements Serializable {

	private static final long serialVersionUID = -4474223917131924506L;
	
	public static final Integer DEFAULT_BUNDLE = 20;
	public static final Integer DEFAULT_PAGE_RANGE = 5;
	public static final Integer DEFAULT_FAST_FORWARD_BUNDLE = 5;
	public static final Integer DEFAULT_FAST_REWIND_BUNDLE = 5;

	protected Integer actualPage;
	protected Integer pageRange;
	protected Integer count;
	protected Integer bundle;

	public DataScroller() {
		this(DEFAULT_BUNDLE);
	}

	public DataScroller(Integer bundle) {
		actualPage = 0;
		count = 0;
		pageRange = DEFAULT_PAGE_RANGE;
		this.bundle = bundle;
	}

	public void previousPage() {
		if (actualPage > 0) {
			actualPage--;
		}
	}

	public void nextPage() {
		if (actualPage < getLastPage()) {
			actualPage++;
		}
	}

	public void fastForward() {
		fastForward(DEFAULT_FAST_FORWARD_BUNDLE);
	}

	public void fastForward(Integer fastForwardBundle) {
		if (actualPage < getLastPage() - fastForwardBundle) {
			actualPage += fastForwardBundle;
		} else {
			lastPage();
		}
	}

	public void fastRewind() {
		fastRewind(DEFAULT_FAST_REWIND_BUNDLE);
	}

	public void fastRewind(Integer fastRewindBundle) {
		if (actualPage > fastRewindBundle) {
			actualPage -= fastRewindBundle;
		} else {
			actualPage = 0;
		}
	}

	public void firstPage() {
		actualPage = 0;
	}

	public void lastPage() {
		actualPage = getLastPage();
	}

	public Integer getLastPage() {
		Integer lastPage = (count / bundle) - 1;
		if (count > bundle && count % bundle > 0)
			lastPage++;
		if (lastPage == -1) {
			lastPage++;
		}
		return lastPage;
	}
	
	public Integer[] getPageList() {
		if (count == 0) return new Integer[0];
		int lastPage = getLastPage();
		int first = 0;
		int end = 0;		
		if (lastPage > (pageRange - 1)) {
			first = actualPage - pageRange / 2;
			if (first < 0) {
				first = 0;
			}
			if (first + pageRange > lastPage) {
				first = lastPage - pageRange + 1;
			}
			end = pageRange;
		} else {
			end = lastPage + 1;
		}
		Integer[] pages = new Integer[end];
		for (int i = 0; i < end; i++) {			
			pages[i] = i + first;
		}
		return pages;
	}

	public Integer getFirstRowIndex() {
		return actualPage * bundle;
	}

	public Integer getFirstRowIndex(int page) {
		return page * bundle;
	}

	public Integer getLastRowIndex() {
		if (count == 0)
			return 0;
		if ((getFirstRowIndex() + bundle) > count) {
			return getFirstRowIndex() + (count % bundle) - 1;
		} else {
			return getFirstRowIndex() + bundle - 1;
		}
	}

	public Integer getLastRowIndex(int page) {
		if (count == 0)
			return 0;
		if ((getFirstRowIndex(page) + bundle) > count) {
			return getFirstRowIndex(page) + (count % bundle) - 1;
		} else {
			return getFirstRowIndex(page) + bundle - 1;
		}
	}

	public String getTextRozsahZaznamu() {
		int firstRowIndex = getFirstRowIndex();
		int lastRowIndex = getLastRowIndex();
		if (firstRowIndex != lastRowIndex) {
			return "Záznam " + (firstRowIndex + 1) + " - " 
					+ (lastRowIndex + 1) + " (celkem: " + getCount() + ")";
		} else {
			return "Záznam " + (firstRowIndex + 1) + " (celkem: " + getCount() + ")";
		}
	}

	public String getTextRozsahStranek() {
		return "Strana " + (actualPage + 1) + " z " + (getLastPage() + 1)
				+ " celkem";
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public Integer getCount() {
		return count;
	}

	public Integer getActualPage() {
		return actualPage;
	}
	
	public void setActualPage(Integer page) {
		this.actualPage = page;
	}

	public void setBundle(Integer bundle) {
		this.bundle = bundle;
	}

	public Integer getBundle() {
		return bundle;
	}

	public Integer getPageRange() {
		return pageRange;
	}

	public void setPageRange(Integer pageRange) {
		this.pageRange = pageRange;
	}

	public Integer getPageItemsCount(int page) {
		Integer pocet = 0;

		if (((page + 1) * bundle) > count) {
			pocet = count - (bundle * (page));
		} else {
			pocet = bundle;
		}

		return pocet;
	}

}
